<template>
  <div class="home">
    <div class="header">
      <h1>校园活动管理系统</h1>
      <div class="user-info">
        <span class="role-badge" :class="{ 'admin': userRole === 'admin' }">
          {{ userRole === 'admin' ? '管理员' : '学生' }}
        </span>
        <button class="logout-btn" @click="handleLogout">退出登录</button>
      </div>
    </div>

    <div class="cards-container">
      <router-link to="/public-activities" class="card">
        <div class="card-icon">🎯</div>
        <h2>公开活动</h2>
        <p>浏览所有可参与的校园活动</p>
      </router-link>

      <div class="card" @click="handleManagementClick">
        <div class="card-icon">⚙️</div>
        <h2>活动管理</h2>
        <p>管理和发布新的校园活动</p>
      </div>
    </div>

    <div v-if="showPermissionAlert" class="alert">
      <div class="alert-content">
        <h3>权限不足</h3>
        <p>只有管理员才能访问活动管理功能</p>
        <button @click="showPermissionAlert = false">确定</button>
      </div>
    </div>
  </div>
</template>

<script>
// @ is an alias to /src
import { resetRouter } from '../router'

export default {
  name: 'HomeView',
  data() {
    return {
      showPermissionAlert: false
    }
  },
  computed: {
    userRole() {
      return localStorage.getItem('userRole') || ''
    }
  },
  methods: {
    handleLogout() {
      localStorage.removeItem('isAuthenticated')
      localStorage.removeItem('userRole')
      resetRouter()
      this.$router.push('/login')
    },
    handleManagementClick() {
      if (this.userRole === 'admin') {
        this.$router.push('/activity-management')
      } else {
        this.showPermissionAlert = true
      }
    }
  }
}
</script>

<style scoped>
.home {
  padding: 2rem;
  max-width: 1200px;
  margin: 0 auto;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 3rem;
  padding: 1rem;
  background: white;
  border-radius: 10px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.header h1 {
  font-size: 1.8rem;
  color: #2c3e50;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.role-badge {
  padding: 0.5rem 1rem;
  border-radius: 20px;
  background-color: #4CAF50;
  color: white;
  font-size: 0.9rem;
}

.role-badge.admin {
  background-color: #2196F3;
}

.logout-btn {
  padding: 0.5rem 1rem;
  background-color: #dc3545;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.logout-btn:hover {
  background-color: #c82333;
}

.cards-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 2rem;
}

.card {
  background: white;
  padding: 2rem;
  border-radius: 10px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  text-decoration: none;
  color: #2c3e50;
  transition: transform 0.3s, box-shadow 0.3s;
  text-align: center;
}

.card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}

.card-icon {
  font-size: 3rem;
  margin-bottom: 1rem;
}

.card h2 {
  margin-bottom: 0.5rem;
  color: #2c3e50;
}

.card p {
  color: #666;
  font-size: 0.9rem;
}

.alert {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.alert-content {
  background: white;
  padding: 2rem;
  border-radius: 10px;
  text-align: center;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
}

.alert-content h3 {
  color: #dc3545;
  margin-bottom: 1rem;
}

.alert-content button {
  margin-top: 1.5rem;
  padding: 0.5rem 2rem;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.alert-content button:hover {
  background-color: #45a049;
}
</style>
